package com.fho4565.core;

import com.fho4565.gui.CustomGUI;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import org.apache.commons.lang3.tuple.MutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.NotNull;

import java.io.Serial;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;

public class DisplayObject implements Serializable ,Comparable<DisplayObject>{
    @Serial
    private static final long serialVersionUID = -6060576390912470439L;
    private String name, objective;
    private Component json = new TextComponent("");
    private DisplayObjectType type = DisplayObjectType.JSON;
    private boolean enabled = true;
    private int x=0,y=0;
    private final HashSet<String> visiblePlayers = new HashSet<>();
    public void setPlayers(Collection<String> collection){
        clearPlayer();
        visiblePlayers.addAll(collection);
    }

    public void clearPlayer(){
        visiblePlayers.clear();
    }
    public boolean isVisible(String name){
        return visiblePlayers.contains(name);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getObjective() {
        return objective;
    }

    public Component getJson() {
        return json;
    }

    public void setJson(Component json) {
        this.json = json;
    }


    public void setObjective(String objective) {
        this.objective = objective;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public Pair<Integer,Integer> getPosition() {
        return MutablePair.of(x,y);
    }

    public void setPosition(int x,int y) {
        this.x = x;
        this.y = y;
    }
    public DisplayObject(String name, String objective, Component json, int x, int y, DisplayObjectType type) {
        this.name = name;
        this.objective = objective;
        this.json = json;
        this.type = type;
        this.x = x;
        this.y = y;
    }
    public DisplayObject(String name, String objective, int x, int y, DisplayObjectType type) {
        this(name, objective, new TextComponent(""), x, y, type);
    }
    public DisplayObject(String name, Component json, int x, int y, DisplayObjectType type) {
        this(name, "", json, x, y, type);
    }

    public DisplayObject(String name, int number, int x, int y, DisplayObjectType type) {
        this(name, "",new TextComponent(""), x, y, type);
    }

    public void render(CustomGUI gui){
        if (!enabled){
            return;
        }
        switch (type) {
            case SCOREBOARD -> gui.drawScore(objective, x, y);
            case JSON -> gui.drawJSON(json, x, y);
        }
    }
    public boolean isEnabled(){
        return enabled;
    }

    @Override
    public int compareTo(@NotNull DisplayObject o) {
        return name.compareTo(o.name);
    }

    public DisplayObjectType getType() {
        return this.type;
    }
}
